<br/>
<h3>list of sites hosted on sitegeist:</h3>

<input/>
<input type=""/>

<?php
?><p/><h4>create database</h4><?php createdb('sitegeist');

include'plugin/base/.db/connect.php';




?><p/><h4>tables</h4><?php tables('p',$db);
?><p/><h4>create table from definition</h4><?php createtabledef($db,'tess','id int,name varchar(56),s blob');
?><p/><h4>create chars</h4><?php $a=array(); $a['url']='ccc'; createchars($db,'tess',$a);

?><p/><h4>columns</h4><?php columns('p',$db,'tess');
?><p/><h4>insert assoc</h4><?php $a=array('name'=>'Renata','s'=>'sssexy');insert($db,'tess',$a);
?><p/><h4>rows</h4><?php $a=array(); rows('p',$db,'tess',array('name'));

?><p/><h4>drop table</h4><?php droptable($db,'tess');

$db=null;

function p($s){ echo"*",$s,"*<br/>"; }

function createdb($name){
	$host='localhost';$port=3307;
	$dbu='root';$dbp='usbw';
	$dsn="mysql:host=$host;port=$port";
	$db=new PDO($dsn,$dbu,$dbp,array( PDO::MYSQL_ATTR_INIT_COMMAND=>"set names utf8" ));
	$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
	$coll='utf8_general_ci'; //utf8_unicode_ci (expansions)
	$name=mysql_real_escape_string($name);
	$q=$db->prepare("create database $name default character set utf8 collate $coll");
	try{ $q->execute(); }
	catch(PDOException $e){	echo$e->getMessage(); }
}

function createtabledef($db,$name,$def,$add=''){
	$name=mysql_real_escape_string($name);
	$def=mysql_real_escape_string($def);
	$add=mysql_real_escape_string($add);
	$defs="create table $name ($def) $add";
	$q=$db->prepare($defs);
	echo$defs;
    $q->execute();
}

function createchars($db,$name,$columns,$unique=null){
	$name=mysql_real_escape_string($name);
	$defs="create table $name (id int,";
	foreach($columns as $col){
		$defs.=mysql_real_escape_string($col).' varchar(56),';
	}
	$defs.='primary key(id),unique(';
	if($unique){
		foreach($unique as $unq){
			$defs.=mysql_real_escape_string($columns[$unq]);
		}
		$defs.='))';
	}
	else $defs.=')';
	echo$defs.' noooo';
	//$q=$db->prepare($defs.')');
    //$q->execute();
}

function droptable($db,$name){
    $q=$db->prepare('drop table '.mysql_real_escape_string($name));

    $q->execute();
}

function insert($db,$table,$data){
    //$cols=implode(",",array_keys($fields)); //insertassoc
	$table=mysql_real_escape_string($table);
	$clean=array();
	foreach(array_keys($data) as $col){
		$clean[mysql_real_escape_string($col)]=mysql_real_escape_string($data[$col]);
	}
	
	$cols=array_map('mysql_real_escape_string',array_keys($data));
	$values=implode(',:',$cols);
	$cols=implode(',',$cols);
	//$defs="insert into $table ($cols) values (:$values)";
	$defs="INSERT INTO `sitegeist`.`tess` (`id`, `name`) VALUES (NULL, 'hello');";
	
	//$defs="INSERT INTO `sitegeist`.`tess` (`id`, `name`) VALUES (NULL, 'hello');"
	
	//$defs='insert into '.$table.' ('.implode(',',array_keys($clean)).') values ('.implode(',',$clean).');';
	echo$defs;
	$q=$db->prepare($defs);
	echo'<br/>xxxxxxx';
	$q->execute();
	
	//$q->execute($clean);

}

function tables($proc,$db,$name=''){
    $q=$db->prepare('show tables '.mysql_real_escape_string($name));
    $q->execute();
    while($row=$q->fetch())$proc($row[0]);
}

function columns($proc,$db,$table){
    $q=$db->prepare('show columns in '.mysql_real_escape_string($table));
    $q->execute();
    while($row=$q->fetch())$proc($row[0]);
}


function rows($proc,$db,$table,$fields,$limit=null){
/*    $fields=implode(',',$fields); //mysql_real_escape_string
	// with
	//array_map ( callback $callback , array $arr1 [, array $... ] )
	$table=mysql_real_escape_string($table);
	$limit=$limit?mysql_real_escape_string($limit):'';
    $q=$db->prepare("select $fields from $table $limit");
    $q->execute();
    while($row=$q->fetch())$proc($row);
	*/
}

function tableswithcolumns($proc,$db,$columns){
    $columns="'".implode("','",$columns)."'";
        echo $columns;
    $q=$db->prepare("select distinct table_name from information_schema.columns where column_name in ($columns) and table_schema='sitegeist'");
    $q->execute();
    while($row=$q->fetch())$proc($row[0]);
}

//http://webcache.googleusercontent.com/search?q=cache:jdoiS1vViAsJ:dev.mysql.com/doc/refman/5.0/en/update.html+mysql+replace+update&cd=1&hl=nl&ct=clnk&gl=be&source=www.google.be
function procselect($table,$names,$limit=""){
    $res=array();
    $q=$db->prepare("SELECT name FROM sg_sites LIMIT 20");
    $q->execute(); //array($_POST['fish'], $_POST['chef'])
    while($result=$q->fetchObject()){
        $result->name;
    }
}

?>

<script type="text/javascript">
    alert("x");
</script>